<?php
// +----------------------------------------------------------------------
// | SparkShop 坚持做优秀的商城系统
// +----------------------------------------------------------------------
// | Copyright (c) 2022~2099 http://sparkshop.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: NickBai  <1902822973@qq.com>
// +----------------------------------------------------------------------

namespace app\admin\controller;

use app\admin\service\DeviceService;
use app\service\CipherService;
use app\model\device\Device as DeviceModel;
use think\facade\View;

class Device extends Base
{
    /**
     * 获取列表
     */
    public function index(DeviceService $deviceService, \app\model\device\DeviceCate $deviceCateModel)
    {
        if (request()->isAjax()) {

            $res = $deviceService->getList(input('param.'))['data'];

            $cate = $deviceCateModel->getAllList([
                'status' => 1
            ], 'id,pid,name')['data']->toArray();

            $list['list'] = $res;
            $list['cate'] = makeTree($cate);

            return jsonReturn(0, 'success', $list);
        }

        return View::fetch();
    }

    /**
     * 添加
     */
    public function add(DeviceService $deviceService)
    {
        if (request()->isPost()) {

            return json($deviceService->addDevice(input('post.'), 'add'));
        }

        if (request()->isAjax()) {

            return jsonReturn(0, 'success', $deviceService->getBaseParam());
        }

        return View::fetch();
    }

    /**
     * 编辑
     */
    public function edit(DeviceService $deviceService,DeviceModel $deviceModel)
    {
        if (request()->isPost()) {

            return json($deviceService->editDevice(input('post.'), 'edit'));
        }

        if (request()->isAjax()) {

            $id = input('param.id');
            $base = [
                'info' => $deviceModel->findOne([
                    'id' => $id
                ])['data']
            ];

            $return = array_merge($base, $deviceService->getBaseParam());

            return jsonReturn(0, 'success', $return);
        }


        return View::fetch();
    }

    /**
     * 上下架
     */
    public function shelf(DeviceModel $deviceModel)
    {
        $param = input('post.');

        $res = $deviceModel->updateByIds([
            'is_show' => $param['is_show']
        ], $param['ids']);

        $res['msg'] = '操作成功';
        return json($res);
    }

    /**
     * 删除商品
     */
    public function del(DeviceService $deviceService)
    {
        return json($deviceService->delDevice(input('param.id')));
    }

    public function recover(DeviceModel $deviceModel)
    {
        $res = $deviceModel->updateByIds([
            'is_del' => 2
        ], input('param.id'));

        $res['msg'] = '操作成功';
        return json($res);
    }
    /**
     * 生成随机密钥
     */
    public function generate(CipherService $cipherService,DeviceModel $deviceModel)
    {

        $result = $cipherService->key_generate(1,0);
        if($result){
            $result = $result[0];
        }else{
            $res['msg'] = '密钥生成失败';
            return json($res);
        }
        $chk = [
            'info' => $deviceModel->findOne([
                'keys' => $result
            ])['data']
        ];
        if($chk['info']){
            $res['keys'] = $result;
            $res['msg'] = '密钥重复请重新生成';
            return json($res);
        }
        $randomBytes = random_bytes(8); // 生成8个字节（64位）的随机值
        $hexString = bin2hex($randomBytes);
        $res['msg'] = '操作成功';
        $res['keys'] = $result;
        $res['randoms'] = $hexString;
        return json($res);
    }
}
